﻿        var map, layer,drawBox,boxLayer,markerLayer,marker,
        url = "http://support.supermap.com.cn:8090/iserver/services/map-world/rest/maps/World",
        styleTest1 = {
                fill:false,
                strokeColor:"blue"
            },
         styleTest2 = {
                fill:false,
                strokeColor:"red"
            };

        var regions_rect = new Array();
        var regions_poly = new Array();
        var rect_bounds = new Array();
        var poly_geo = new Array();

        var areajson;
        var Interval;

        var infowin = null;

        function init(){

            boxLayer = new SuperMap.Layer.Vector("boxLayer");
            polygonLayer = new SuperMap.Layer.Vector("polygonLayer");
            markerLayer = new SuperMap.Layer.Markers("makerLayer");

            boxLayer.style = styleTest1;
            polygonLayer.style =styleTest2;
            
            drawBox = new SuperMap.Control.DrawFeature(boxLayer,SuperMap.Handler.Box);
            drawPolygon = new SuperMap.Control.DrawFeature(polygonLayer,SuperMap.Handler.Polygon);
                      
            map = new SuperMap.Map("map",{controls:[
                new SuperMap.Control.Zoom() ,
                new SuperMap.Control.Navigation() ,
                new SuperMap.Control.LayerSwitcher()
            ,drawBox,drawPolygon]});

            map.addControl(new SuperMap.Control.MousePosition());

            layer = new SuperMap.Layer.TiledDynamicRESTLayer("World",url,null,{maxResolution:"auto"});
            
            layer.events.on({"layerInitialized":addLayer});
               
            drawBox.events.on({"featureadded":drawBoxCompleted});
            drawPolygon.events.on({"featureadded":drawPolygonCompleted});

            
        }

        function addLayer(){
            map.addLayers([layer,boxLayer,polygonLayer,markerLayer]);
            map.setCenter(new SuperMap.LonLat(0,0),0);
        }

        function drawBoxCompleted(eventArgs){
            var geometry1 = eventArgs.feature.geometry;
            var bounds1 = geometry1.bounds.toArray();

            var info1 = '{"dr_latitude":' + bounds1[1]
                        + ',"dr_longitude":' + bounds1[2]
                        + ', "ul_latitude": ' + bounds1[3] +
                        ', "ul_longitude":' + bounds1[0] + '}';

            regions_rect.push(info1);
            rect_bounds.push(bounds1);
        }

        function drawPolygonCompleted(eventArgs){
            var geometry2 = eventArgs.feature.geometry;
            var bounds2 = geometry2.bounds.toArray();

            var info2 = '{"dr_latitude":' + bounds2[1]
                + ',"dr_longitude":' + bounds2[2]
                + ', "ul_latitude": ' + bounds2[3] +
                ', "ul_longitude":' + bounds2[0] + '}';
            
            regions_poly.push(info2);
            poly_geo.push(geometry2);
        }

        function draw_box(){
            deactiveAll();
            drawBox.activate();
        }

        function draw_polygon(){
            deactiveAll();
            drawPolygon.activate();
        }

        function deactiveAll(){
            drawPolygon.deactivate();
            drawBox.deactivate();
        }

        function clearFeatures(){
            deactiveAll();
            boxLayer.removeAllFeatures();
            polygonLayer.removeAllFeatures();
            markerLayer.clearMarkers();
            regions_rect = [];
            regions_poly = [];
            rect_bounds = [];
            poly_geo = [];
            clearInterval(Interval);
            document.getElementById("data").innerHTML = "";
        }


        function getArea(){
            deactiveAll();
            var jsons =[];
            for(var i=0;i<regions_rect.length;i++){
                var json = regions_rect[i];
                jsons.push(json);
            }
            for( i=0;i<regions_poly.length;i++){
                var json = regions_poly[i];
                jsons.push(json);
            }
            areajson='{"areas":[' + jsons.toString() + ']}';

            var second = 30000;
            Interval = setInterval('warning()',second);
        }

        function warning(){
            markerLayer.clearMarkers();
            document.getElementById("data").innerHTML = "";
            $.ajax({
                type:"GET",
                url:"/alert/summary/getSquareSummary.do",
                data: {
                    list:areajson
                },
                dataType:"json",
                success:function(data){
                    if(regions_rect.length > 0){
                        for( var i=0;i<regions_rect.length;i++){
                            for( var j=0;j<data.msg.list[i].length;j++) {
                                var lon = data.msg.list[i][j].longitude / 600000;
                                var lat = data.msg.list[i][j].latitude / 600000;
                                if(lon > rect_bounds[i][0] && lon < rect_bounds[i][2] && lat > rect_bounds[i][1] && lat < rect_bounds[i][3]){
                                    addData(lon,lat);
                                    document.getElementById("data").innerHTML +="lon:"+lon+"lat:"+lat+"\n";
                                }
                            }
                        }
                    }
                    if(regions_poly.length > 0){
                        for(var i=regions_rect.length;i<regions_rect.length+regions_poly.length;i++){
                            for(var j=0;j<data.msg.list[i].length;j++){
                                var lon = data.msg.list[i][j].longitude/600000;
                                var lat = data.msg.list[i][j].latitude/600000;
                                var point = new SuperMap.Geometry.Point(lon, lat);
                                if(point.intersects(poly_geo[i-regions_rect.length]) == true){
                                    addData(lon,lat);
                                    document.getElementById("data").innerHTML +="lon:"+lon+"lat:"+lat+"\n";
                                }
                            }
                        }
                    }
                },
                error:function(XMLHttpRequest){
                    alert(XMLHttpRequest.statusText);
                }
            })
        }

        function addData(Lon,Lat){
            //markerLayer.removeMarker(marker);
            var size = new SuperMap.Size(24,18);
            var offset = new SuperMap.Pixel(-(size.w/2), -size.h);
            var icon = new SuperMap.Icon('./theme/images/marker.png', size, offset);
            marker =new SuperMap.Marker(new SuperMap.LonLat(Lon,Lat),icon) ;

            marker.events.on({
                "click":openInfoWin,
                "touchstart":openInfoWin,
                "scope":marker
            });

            markerLayer.addMarker(marker);
        }

        function openInfoWin(){
            closeInfoWin();
            var marker =this;
            var lonlat = marker.getLonLat();
            var size = new SuperMap.Size(0,18);
            var offset = new SuperMap.Pixel(5,-15);
            var icon = new SuperMap.Icon("./theme/images/marker.png",size,offset);
            var popup = new SuperMap.Popup.FramedCloud("popwin",new SuperMap.LonLat(lonlat.lon,lonlat.lat),null,"("+lonlat.lon+","+lonlat.lat+")",icon,true);
            infowin = popup;
            map.addPopup(popup);
        }

        function closeInfoWin(){
            if(infowin){
                try{
                    infowin.hide();
                    infowin.destroy();
                }
                catch(e){}
            }
        }